#include "setting.h"
#include "ui_setting.h"
#include "QPushButton"
#include <QSound>

Setting::Setting(QWidget *parent) :
    QDialog(parent),
    settings("settings.ini", QSettings::IniFormat),
    ui(new Ui::Setting)
{
    ui->setupUi(this);
    setWindowTitle("设置");
    QIcon *icon = new QIcon(":/icon/res/tea.png");
    setWindowIcon(*icon);
    setWindowFlags(Qt::CustomizeWindowHint | Qt::WindowCloseButtonHint);
}

Setting::~Setting()
{
    delete ui;
}

void Setting::closeEvent(QCloseEvent *event)
{
    this->hide();
    event->ignore();
}

void Setting::showEvent(QShowEvent *event)
{
    auto v0 = settings.value("interval/0", 30).toInt();
    auto v1 = settings.value("interval/1", 1).toInt();
    auto v2 = settings.value("play_sound", 0).toInt();
    auto v3 = settings.value("auto_pause", false).toBool();
    auto v4 = settings.value("auto_continue", 0).toBool();

    if (v0 == 10) {
        this->ui->cb1->setCurrentIndex(0);
    } else if (v0 == 15) {
        this->ui->cb1->setCurrentIndex(1);
    } else if (v0 == 20) {
        this->ui->cb1->setCurrentIndex(2);
    } else if (v0 == 25) {
        this->ui->cb1->setCurrentIndex(3);
    } else if (v0 == 30) {
        this->ui->cb1->setCurrentIndex(4);
    } else if (v0 == 45) {
        this->ui->cb1->setCurrentIndex(5);
    } else if (v0 == 60) {
        this->ui->cb1->setCurrentIndex(6);
    } else if (v0 == 90) {
        this->ui->cb1->setCurrentIndex(7);
    } else if (v0 == 120) {
        this->ui->cb1->setCurrentIndex(8);
    }

    if (v1 == 1) {
        this->ui->cb2->setCurrentIndex(0);
    } else if (v1 == 5) {
        this->ui->cb2->setCurrentIndex(1);
    } else if (v1 == 10) {
        this->ui->cb2->setCurrentIndex(2);
    } else if (v1 == 15) {
        this->ui->cb2->setCurrentIndex(3);
    } else if (v1 == 30) {
        this->ui->cb2->setCurrentIndex(4);
    }

    this->ui->cbSound->setCurrentIndex(v2);

    if (v3) {
        this->ui->cbAutoPause->setChecked(true);
    } else {
        this->ui->cbAutoPause->setChecked(false);
    }

    if (v1 == 60 * 5) {
        this->ui->cb3->setCurrentIndex(60 * 5);
    } else if (v1 == 60 * 10) {
        this->ui->cb3->setCurrentIndex(60 * 10);
    } else if (v1 == 60 * 15) {
        this->ui->cb3->setCurrentIndex(60 * 15);
    } else if (v1 == 60 * 20) {
        this->ui->cb3->setCurrentIndex(60 * 20);
    } else {
        this->ui->cb3->setCurrentIndex(0);
    }
}

void Setting::on_pbOk_clicked()
{
    if (this->ui->cb1->currentIndex()==0) {
        settings.setValue("interval/0", 10);
    } else if (this->ui->cb1->currentIndex()==1) {
        settings.setValue("interval/0", 15);
    } else if (this->ui->cb1->currentIndex()==2) {
        settings.setValue("interval/0", 20);
    } else if (this->ui->cb1->currentIndex()==3) {
        settings.setValue("interval/0", 25);
    } else if (this->ui->cb1->currentIndex()==4) {
        settings.setValue("interval/0", 30);
    } else if (this->ui->cb1->currentIndex()==5) {
        settings.setValue("interval/0", 45);
    } else if (this->ui->cb1->currentIndex()==6) {
        settings.setValue("interval/0", 60);
    } else if (this->ui->cb1->currentIndex()==7) {
        settings.setValue("interval/0", 90);
    } else if (this->ui->cb1->currentIndex()==8) {
        settings.setValue("interval/0", 120);
    }

    if (this->ui->cb2->currentIndex()==0) {
        settings.setValue("interval/1", 1);
    } else if (this->ui->cb2->currentIndex()==1) {
        settings.setValue("interval/1", 5);
    } else if (this->ui->cb2->currentIndex()==2) {
        settings.setValue("interval/1", 10);
    } else if (this->ui->cb2->currentIndex()==2) {
        settings.setValue("interval/1", 15);
    } else if (this->ui->cb2->currentIndex()==2) {
        settings.setValue("interval/1", 30);
    }

    if (this->ui->cb3->currentIndex()==0) {
        settings.setValue("auto_continue", 0);
    } else if (this->ui->cb3->currentIndex()==1) {
        settings.setValue("auto_continue", 5 * 60);
    } else if (this->ui->cb3->currentIndex()==2) {
        settings.setValue("auto_continue", 10 * 60);
    } else if (this->ui->cb3->currentIndex()==2) {
        settings.setValue("auto_continue", 15 * 60);
    } else if (this->ui->cb3->currentIndex()==2) {
        settings.setValue("auto_continue", 20 * 60);
    }

    if (this->ui->cbSound->currentIndex()==0) {
        settings.setValue("play_sound", 0);
    } else if (this->ui->cbSound->currentIndex()==1) {
        settings.setValue("play_sound", 1);
    } else if (this->ui->cbSound->currentIndex()==2) {
        settings.setValue("play_sound", 2);
    } else if (this->ui->cbSound->currentIndex()==3) {
        settings.setValue("play_sound", 3);
    } else if (this->ui->cbSound->currentIndex()==4) {
        settings.setValue("play_sound", 4);
    } else if (this->ui->cbSound->currentIndex()==5) {
        settings.setValue("play_sound", 5);
    }

    if (this->ui->cbAutoPause->checkState()) {
        settings.setValue("auto_pause", true);
    } else {
        settings.setValue("auto_pause", false);
    }
    this->hide();
}

void Setting::on_pbCancel_clicked()
{
    this->hide();
}

void Setting::on_cbSound_currentIndexChanged(int v)
{
    // 第一次不用
    if (!soundInited) {
        soundInited = true;
    } else if (v > 0) {
        QSound::play(QString(":/sound/res/rest%1.wav").arg(v));
    }
}

